angular.module('SpecialJudgment')

.controller('SpecialJudgment.HomeCtrl', ['$scope', '$state', 'SpecialJudgmentHomeService', 'User', 'imanDialog', '$ionicLoading', 'imanUsbPort', function($scope, $state, SpecialJudgmentHomeService, User, imanDialog, $ionicLoading, imanUsbPort) {
    var uuid = '';
    var Barcode = ''; // 条码标识
    var TerminalUniqueID = ''; // 设备标识
    $scope.addNote = ''; //备注
    $scope.data = {};

    function getBarCodeData(code) {
        imanDialog.showLoading();
        SpecialJudgmentHomeService.getPersonOrMaterielData(code).then(function(data) {
            imanDialog.hideLoading();
            if (data != null) {
                $scope.data = data;
            } else {
                imanDialog.alert("单品数据为空", "");
            }
        }, function(error) {
            imanDialog.hideLoading();
            $scope.data = {};
        });
    }

    $scope.submitData = function(e) {
        uuid = 'TC0000001'; // 条码标识
        imanDialog.showLoading();
        SpecialJudgmentHomeService.submitData(Barcode, uuid, User.get("_id"), e, $scope.addNote).then(function(data) {
            imanDialog.hideLoading();
            if (data) {
                imanDialog.alert("特裁成功", "");
            } else {
                imanDialog.alert("特裁失败", "");
            }
        }, function(res) {
            imanDialog.hideLoading();
        });
    };

    $scope.prompt = function() {
        imanDialog.prompt('请输入文本', '备注').then(function(text) {
            $scope.addNote = text; //备注
        });
    };

    $scope.$on('$ionicView.beforeEnter', function() {
        imanUsbPort.start();
    });
    $scope.$on('$ionicView.afterLeave', function() {
        imanUsbPort.stop();
    });
    $scope.$on('imanUsbPort.input', function(e, inputStr) {
        imanDialog.toastBottom(inputStr);
        Barcode = inputStr;
        getBarCodeData(Barcode);
    });
}]);